﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using VMind.Global;

namespace PPIDesktopClient.General
{
    public class XMLHelperDownloadImage
    {
        protected XmlDocument m_XmlDocument;
        private string m_FileName;

        public XMLHelperDownloadImage(string xml)
        { 
            //Load xml File
            m_XmlDocument = new XmlDocument();
            m_FileName = xml;
            if (File.Exists(xml))
            {
                m_XmlDocument.Load(xml);
            }
            else
            {
                XmlNode _DocumentNode = m_XmlDocument.CreateXmlDeclaration("1.0", "UTF-8", null);
                m_XmlDocument.AppendChild(_DocumentNode);

                _DocumentNode = m_XmlDocument.CreateElement("Images");
                m_XmlDocument.AppendChild(_DocumentNode);

            }
        }

        public virtual void AddImageNode(int structureID, int imageId, string name, string fullpath)
        {
            XmlNode _Node = CreateImageNode(structureID, imageId, name, fullpath);
            m_XmlDocument.ChildNodes[1].AppendChild(_Node);     
        }

        protected virtual XmlNode CreateImageNode(int structureID, int imageId, string name, string fullpath)
        {
            XmlNode _Node = m_XmlDocument.CreateElement("Image");
            XmlAttribute _NodeAttribute = m_XmlDocument.CreateAttribute("ID");
            _NodeAttribute.Value = imageId.ToString();
            _Node.Attributes.Append(_NodeAttribute);

            XmlNode _Child = m_XmlDocument.CreateElement("StructureID");
            _Child.AppendChild(m_XmlDocument.CreateTextNode(structureID.ToString()));
            _Node.AppendChild(_Child);

            _Child = m_XmlDocument.CreateElement("Name");
            _Child.AppendChild(m_XmlDocument.CreateTextNode(name));
            _Node.AppendChild(_Child);

            _Child = m_XmlDocument.CreateElement("Path");
            _Child.AppendChild(m_XmlDocument.CreateTextNode(fullpath));
            _Node.AppendChild(_Child);
            return _Node;
        }

        public virtual void Save()
        {
            m_XmlDocument.Save(m_FileName);
        }

        public int GetLastImageID()
        {
            int m_returnValue = 0;

            if(m_XmlDocument.ChildNodes.Count >1)
            {
                if (m_XmlDocument.ChildNodes[1].LastChild != null)
                    m_returnValue = ConvertHelper.ToInt32(m_XmlDocument.ChildNodes[1].LastChild.Attributes["ID"].Value);
            }
            return m_returnValue;
        }

        public void DeleteImages(int structureID)
        {
            XmlNodeList _ImageNodes = m_XmlDocument.SelectNodes(@"//Image[StructureID='" + structureID + "']");
            FileInfo _FileImage;
            foreach (XmlNode n in _ImageNodes)
            {
                _FileImage = new FileInfo(n["Path"].InnerText);
                if (_FileImage.Exists)
                {
                    _FileImage.Delete();
                    n.ParentNode.RemoveChild(n);
                }                   
            }
        }
    }
}
